package com.bytecollege.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bytecollege.mapper.CourseMapper;
import com.bytecollege.model.Course;
import com.bytecollege.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @ClassName: CourseServiceImpl
 * @Description: 课程服务层接口
 * @Author: Sun
 * @Date: 2022/10/18 21:03
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询所有课程
     *
     * @param page
     * @return
     */
    public List<Course> getAll(Page<Course> page) {
        courseMapper.selectPage(page, null);
        return page.getRecords();
    }

    /**
     * 查询单个课程
     *
     * @param id
     * @return
     */
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

    /**
     * 点击量
     *
     * @param course
     * @return
     */
    public Integer modClicks(Course course) {
        return courseMapper.modClicks(course);
    }

    /**
     * 获取VIP课程
     *
     * @param page
     * @return
     */
    public List<Course> getVIP(Page<Course> page) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        courseMapper.selectPage(page, wrapper.eq("course_status", 1));
        return page.getRecords();
    }

    /**
     * 查询免费课程
     *
     * @param page
     * @return
     */
    public List<Course> getFree(Page<Course> page) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        courseMapper.selectPage(page, wrapper.eq("course_status", 0));
        return page.getRecords();
    }

    public List<Course> getByCtypeId(Page<Course> page,Integer ctypeId) {
        courseMapper.selectPage(page,new QueryWrapper<Course>().eq("ctype_id",ctypeId));
        return page.getRecords();
    }


    /**
     * 搜索和关键词过滤
     * @param isSearch 是否使用搜索
     * @param search 搜索内容
     * @param isVip 是否查询vip状态
     * @param vipCode vip状态0免费，1会员
     * @param isType 是否根据类型查询
     * @param value 类型id
     * @param isOrder 是否使用排序（降序）
     * @param orderFiled 排序字段
     * @param page 页面信息
     * @return
     */
    public List<Course> getSearch(boolean isSearch,
                                  String search,
                                  boolean isVip,
                                  String vipCode,
                                  boolean isType,
                                  Object value,
                                  boolean isOrder,
                                  String orderFiled,
                                  Page<Course> page
    ) {
        courseMapper.selectPage(page, new QueryWrapper<Course>()
                .eq(isVip, "course_status", vipCode)
                .eq(isType, "ctype_id", value)
                .orderBy(isOrder, false, orderFiled)
                .like(isSearch, "course_name", search)
                .or()
                .like(isSearch, "course_describe", search)
        );

        return page.getRecords();
    }


}